package com.aspose.words.maven.wizard;


import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

/**
 * @author Affan Hasan
 *
 */
public class AsposeWordMavenProjectWizard extends Wizard implements INewWizard {

	private AsposeWordMavenProjectWizardPage asposeWizardPage;

	public AsposeWordMavenProjectWizard() {
		setWindowTitle("Aspose.Words Maven Project");
	}

	@Override
	public void addPages() {
		asposeWizardPage = new AsposeWordMavenProjectWizardPage();
		addPage(asposeWizardPage);
	}

	@Override
	public boolean performFinish() {
		
		final IWorkspace workspace = ResourcesPlugin.getWorkspace();
		final IWorkspaceRoot iWorkspaceRoot = workspace.getRoot();
		final IProject iProject = iWorkspaceRoot.getProject(asposeWizardPage.getProjectName().getText());
		if (!iProject.exists()) {
			try {
				iProject.create(null);
				iProject.open(null);
//				 Setting project nature as Maven & Java
				IProjectDescription description = iProject.getDescription();
				String[] natures = description.getNatureIds();
				String[] newNatures = new String[natures.length + 2];
				System.arraycopy(natures, 0, newNatures, 0, natures.length);
				newNatures[natures.length] = "org.eclipse.m2e.core.maven2Nature";
				newNatures[natures.length + 1] = "org.eclipse.jdt.core.javanature";
				description.setNatureIds(newNatures);
				ResourcesPlugin.getWorkspace().validateNatureSet(newNatures);
				iProject.setDescription(description, null);
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
		
		try {
			IJavaProject jp = JavaCore.create(iProject);
//			Including folders in build path
			IClasspathEntry[] buildPath = {
					JavaCore.newSourceEntry(iProject.getFullPath().append("src/main/java")),
					JavaCore.newSourceEntry(iProject.getFullPath().append("src/test/java")),
					JavaRuntime.getDefaultJREContainerEntry() };
			jp.setRawClasspath(buildPath, iProject.getFullPath().append("target/classes"), null);
			
//			Getting the Json based maven project structure from template ( For template please refer to file "maven_project_structure.ftl" )
			final JsonObject projectStructure = new Gson().fromJson(ResourceFactory.getMavenProjectStructureTemplate(), JsonObject.class);
			final MavenProjectGenerationUtility mavenProjectGenerationUtility = new MavenProjectGenerationUtility();
			mavenProjectGenerationUtility.generateDirectoryStructure(iProject, new StringBuilder(), projectStructure);
//			Generating source packages
			mavenProjectGenerationUtility.generateJavaPackages(jp, iProject.getFolder("src/main/java"), asposeWizardPage.getPackageName().getText());			
//			Generating the pom.xml file
			mavenProjectGenerationUtility.generatePomFile(iProject, 
					this.asposeWizardPage.getGroupId().getText(), 
					this.asposeWizardPage.getArtifactId().getText(), 
					this.asposeWizardPage.getVersion().getText());
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
	}
}